import React, { Component } from "react";
// 引入 echarts 核心模块
import * as echarts from 'echarts/core';
// 引入柱状图图表
import { BarChart } from 'echarts/charts';
// 引入组件
import {
  TitleComponent,
  TooltipComponent,
  GridComponent,
  DatasetComponent,
  TransformComponent
} from 'echarts/components';
// 引入特性
import { LabelLayout, UniversalTransition } from 'echarts/features';
// 引入渲染器
import { CanvasRenderer } from 'echarts/renderers';

// 注册必须的组件
echarts.use([
  TitleComponent,
  TooltipComponent,
  GridComponent,
  DatasetComponent,
  TransformComponent,
  BarChart,
  LabelLayout,
  UniversalTransition,
  CanvasRenderer
]);

export default class App extends Component {
  private chartInstance: echarts.ECharts | null = null;

  componentDidMount(): void {
    const chartDom = document.getElementById('main');
    if (chartDom) {
      this.chartInstance = echarts.init(chartDom);
      this.chartInstance.setOption({
        title: {
          text: 'ECharts 入门示例'
        },
        tooltip: {},
        xAxis: {
          data: ['衬衫', '羊毛衫', '雪纺衫', '裤子', '高跟鞋', '袜子']
        },
        yAxis: {},
        series: [
          {
            name: '销量',
            type: 'bar',
            data: [5, 20, 36, 10, 10, 20]
          }
        ]
      });
    }
  }

  componentWillUnmount(): void {
    if (this.chartInstance) {
      this.chartInstance.dispose(); // 销毁图表实例
      this.chartInstance = null;
    }
  }

  render() {
    return (
      <div id="main" style={{ width: '600px', height: '400px' }}>
        <h1>父组件</h1>
      </div>
    );
  }
}